import { _decorator, Component, isValid, Node, VideoPlayer } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('WeChatVideo')
export class WeChatVideo extends Component {
    private _wsVideo: any = null;
    private _currentTime: number = 0;
    private _duration: number = 0;
    private _isPlaying: boolean = false;
    private _videoUrl: string = "";
    private _tempUrl: string = "";
    private _isLoop: boolean = false;

    onDestroy(): void {
        this.stop();
    }
     onEnable(): void {
        this.createVideo("https://sxres.xhhycd.com/IAA/mv_nixi_mma/mma_hh_jd1_1.mp4", true);
    }
    showVideo(): void {
        this.createVideo("https://sxres.xhhycd.com/IAA/mv_nixi_mma/mma_hh_jd1_1.mp4", true);
    }

    async createVideo(
        url: string,
        loop: boolean = false,
        autoplay: boolean = true,
        underGameView: boolean = true
    ): Promise<void> {
        if (<any>window.wx) {
            if (this._wsVideo) {
                this.removeListener();
                this._wsVideo.destroy();
            }

            this._isLoop = loop;

            this._tempUrl = url;

            this._videoUrl = url;

            this._wsVideo = wx.createVideo({
                x: 0,
                y: 0,
                width: window.innerWidth,
                height: window.innerHeight,
                underGameView,
                showCenterPlayBtn: false,
                showProgressInControlMode: false,
                showProgress: false,
                controls: false,
                obeyMuteSwitch: true,
                autoplay,
                objectFit: "cover",
                enableProgressGesture: false,
                loop,
                initialTime: 0,
                backgroundColor: "#00000000",
                volume: 1,
                src: this._tempUrl,
            });

            this.addListener();
        }
    }
    seek(event: any, time: string): void {
        this._wsVideo?.seek(Number(time));
    }

    pause(): void {
        this._wsVideo?.pause();
    }

    play(): void {
        this._wsVideo?.play();
    }

    rePlay(url: string): void {
        this.createVideo(url, true);
    }

    stop(): void {
        if (this._wsVideo) {
            this._wsVideo.stop();
            this._wsVideo.destroy();
            this._wsVideo = null;
            this.removeListener();
        }
    }

    addListener(): void {
        if (this._wsVideo) {
            this._wsVideo.onError(this._onError.bind(this));
            this._wsVideo.onPlay(this._onPlay.bind(this));
            this._wsVideo.onEnded(this._onEnded.bind(this));
            this._wsVideo.onTimeUpdate(this._onTimeUpdate.bind(this));
            this._wsVideo.onWaiting(this._onWaiting.bind(this));
            this._wsVideo.onPause(this._onPause.bind(this));
        }
    }

    removeListener(): void {
        if (this._wsVideo) {
            this._wsVideo.offError(this._onError.bind(this));
            this._wsVideo.offPlay(this._onPlay.bind(this));
            this._wsVideo.offEnded(this._onEnded.bind(this));
            this._wsVideo.offTimeUpdate(this._onTimeUpdate.bind(this));
            this._wsVideo.offWaiting(this._onWaiting.bind(this));
            this._wsVideo.offPause(this._onPause.bind(this));
        }
    }



    private _onError(event: any): void {
        if (isValid(this.node)) {
            this.node.emit(VideoPlayer.EventType.ERROR, event.errMsg);
        }
        console.log("_onError---------------")
    }

    private _onPlay(event: any): void {
        if (isValid(this.node)) {
            this.node.emit(VideoPlayer.EventType.READY_TO_PLAY, this);
        }
        this._isPlaying = true;
        console.log("_onPlay---------------")
    }

    private _onEnded(event: any): void {
        this._isPlaying = false;
        if (isValid(this.node)) {
            this.node.emit(VideoPlayer.EventType.COMPLETED, this);
        }
        if (this._isLoop) {
            this.play();
        }
        console.log("_onEnded---------------")
    }

    private _onTimeUpdate(event: any): void {
        if (this._currentTime === 0 && event.position > 0 && isValid(this.node)) {
            this.node.emit(VideoPlayer.EventType.PLAYING, this);
        }
        this._currentTime = event.position;
        this._duration = event.duration;
        console.log("_onTimeUpdate---------------")
    }

    private _onWaiting(event: any): void {
        // Handle waiting event if needed
        console.log("_onWaiting---------------")
    }

    private _onPause(event: any): void {
        this._isPlaying = false;
        if (isValid(this.node)) {
            this.node.emit(VideoPlayer.EventType.PAUSED, this);
        }
        console.log("_onPause---------------")
    }

    get currentTime(): number {
        return this._currentTime;
    }

    get duration(): number {
        return this._duration;
    }

    get isPlaying(): boolean {
        return this._isPlaying;
    }
}



